package com.blog.jdo;

import java.util.Collection;
import java.util.Date;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import org.apache.log4j.Logger;

import com.blog.model.Article;

public class ArticleDAO {
	private static final Logger log = Logger.getLogger(Article.class);
	
	public void deleteArticle(final Article article) {
		final String logString = "deleteArticle( " + article + " )";
		log.trace(logString + " - begin");
		final PersistenceManager pm = PMF.get().getPersistenceManager();
		final Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.deletePersistent(article);
			tx.commit();
		}
		catch (final Exception e) {
			log.error(logString + " - exception", e);
			throw new RuntimeException(e);
		}
		finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
		log.trace(logString + " - end");
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Article> getArticles() {
		final String logString = "getArticles()";
		log.trace(logString + " - begin");
		Collection<Article> retVal;
		final PersistenceManager pm = PMF.get().getPersistenceManager();
		final Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			final Query q = pm.newQuery(Article.class);
			final Collection<Article> result = (Collection<Article>) q.execute();
			retVal = result;
			tx.commit();
		}
		catch (final Exception e) {
			log.error(logString + " - exception", e);
			throw new RuntimeException(e);
		}
		finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
		log.trace(logString + " - end ( returning: " + retVal + " )");
		return retVal;
	}
	
	public void saveArticle(final Article article) {
		final String logString = "saveArticle( " + article + " )";
		log.trace(logString + " - begin");
		final Article retVal = null;
		final PersistenceManager pm = PMF.get().getPersistenceManager();
		final Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			Article a;
			try {
				if (article.getId() == null) {
					a = new Article();// TODO Auto-generated method stub
					
				}
				else {
					a = pm.getObjectById(Article.class, article.getId());
				}
			}
			catch (final JDOObjectNotFoundException e) {
				a = new Article();
			}
			
			a.setSource(article.getSource());
			a.setTitle(article.getTitle());
			a.setCompiled(article.isCompiled());
			a.setLastUpdate(new Date());
			
			pm.makePersistent(a);
			tx.commit();
		}
		catch (final Exception e) {
			log.error(logString + " - exception", e);
			throw new RuntimeException(e);
		}
		finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
		log.trace(logString + " - end ( returning: " + retVal + " )");
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Article> getUncompiledArticles() {
		final String logString = "getUncompiledArticles()";
		log.trace(logString + " - begin");
		Collection<Article> retVal;
		final PersistenceManager pm = PMF.get().getPersistenceManager();
		final Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			final Query q = pm.newQuery(Article.class, "compiled == compiledParam");
			q.declareParameters("Boolean compiledParam");
			final Collection<Article> result = (Collection<Article>) q.execute(Boolean.FALSE);
			retVal = result;
			tx.commit();
		}
		catch (final Exception e) {
			log.error(logString + " - exception", e);
			throw new RuntimeException(e);
		}
		finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
		log.trace(logString + " - end ( returning: " + retVal + " )");
		return retVal;
	}
	
}
